package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.BlogDetail;
import com.hemaapp.yjnh.view.NumberSelectorLayout;

import java.math.BigDecimal;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;

/**
 * 农货商品立即购买第一步
 *
 */
public class FarmerCropBuyActivity1 extends BaseActivity implements OnClickListener {

    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.layout_number_selector)
    NumberSelectorLayout numberSelector;
    @Bind(R.id.tv_total)
    TextView tvTotal;
    @Bind(R.id.tv_commit)
    TextView tvCommit;

    private BlogDetail blog;//当前商品
    private Double price = 1.0;
    private Double total = 0.0;//总费用
    private int stock = 0;//库存量
    private int buynum = 1;//够买数量

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_buy_addcart);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getExras() {
        blog = (BlogDetail) mIntent.getSerializableExtra("blog");
    }
    @Override
    protected void findView() {
    }

    @Override
    protected void setListener() {
        titleText.setText("立即购买");
        titleRightBtn.setVisibility(View.GONE);
        numberSelector.setNumberSelectorListener(new NumberSelectorLayout.NumberSelectorListener() {
            @Override
            public void onNumberChanged(int curValue) {
                buynum = curValue;
                try {
                    total = buynum * price;
                    tvTotal.setText("¥" + uuUtils.formatAfterDot2(total + ""));
                }catch (Exception e) {
                    e.printStackTrace();
                    numberSelector.setNumber(1);
                }
            }
        });
        if (blog != null) {
            price = Double.valueOf(blog.getPrice());
            tvName.setText(blog.getName());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(price+""));
            total = price;
            tvTotal.setText("¥" + uuUtils.formatAfterDot2(total+""));
            if (blog.getLeftcount() != null) {
                stock = Integer.parseInt(blog.getLeftcount());
            }
        }
        if (stock < 1) {//如果库存小于1，就显示不可购买
            XtomToastUtil.showShortToast(mContext, "库存不足");
            tvCommit.setEnabled(false);
        }else {
            numberSelector.setMaxValue(stock);
        }
        numberSelector.setNumber(1);
        numberSelector.setEditable(true);
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CART_ADD:
                showProgressDialog("请稍候");
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CART_ADD:
                cancelProgressDialog();
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CART_ADD:
                showTextDialog("成功添加到购物车!");
                titleText.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        setResult(RESULT_OK);
                        finish();
                    }
                }, 1000);
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CART_ADD:
                showTextDialog(baseResult.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CART_ADD:
                showTextDialog("添加购物车失败!");
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.title_left_btn, R.id.tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.tv_commit:
                if (buynum < 1) {
                    XtomToastUtil.showShortToast(mContext, "请输入购买数量");
                    break;
                }
                Intent intent = new Intent(mContext, BuyConfirmActivityNew.class);
                intent.putExtra("keytype", "2");// 订单方式	1：从购物车中来 2：立即购买 3：定制订单 4：走走订单
                intent.putExtra("blog", blog);// 够买商品
                intent.putExtra("buycount", buynum);// 够买商品数量
                startActivity(intent);
                finish();
                break;
        }
    }
}
